package com.wqiuqiu.gulimall.cart.controller;


import com.wqiuqiu.common.R;
import com.wqiuqiu.gulimall.cart.interceptor.CartInterceptor;
import com.wqiuqiu.gulimall.cart.service.CartService;
import com.wqiuqiu.gulimall.cart.vo.Cart;
import com.wqiuqiu.gulimall.cart.vo.CartItemVo;
import com.wqiuqiu.gulimall.cart.vo.CartVo;
import com.wqiuqiu.gulimall.cart.vo.UserInfoTo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.websocket.server.PathParam;
import java.util.List;
import java.util.concurrent.ExecutionException;

@Controller
public class CartController {

    @Autowired
    CartService cartService;

    @RequestMapping("/cartList.html")
    public String cartList(Model model) {
        UserInfoTo userInfoTo = CartInterceptor.threadLocal.get();
        CartVo cart = cartService.getCartList(userInfoTo);
        model.addAttribute("cart",cart);
        return "cartList";
    }

    @RequestMapping("/addCart")
    public String addCart(@RequestParam("skuId") Long skuId, @RequestParam("num") Integer num) throws ExecutionException, InterruptedException {
        CartItemVo vo = cartService.addCart(skuId, num);
        return "redirect:http://cart.gulimall.com/success.html?skuId=" + skuId;
    }

    @GetMapping("/success.html")
    public String addCartSuccess(@PathParam("skuId") Long skuId, Model model) {
        CartItemVo vo = cartService.getCart(skuId);
        model.addAttribute("item", vo);
        return "success";
    }




}
